﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using RuiXinCoffee.Model;

namespace RuiXinCoffee.DAL
{
    internal class OrderDAO : BaseDAO<OrderInfo>
    {
        //添加订单
        public int AddOrder(OrderInfo order)
        {
            string sql = "INSERT INTO OrderInfo (ProductId, ProductImageUrl, ProductName, CupType, SugarLevel, IceLevel, Quantity, Price, TotalPrice, OrderStatus) " +
                         "VALUES (@ProductId, @ProductImageUrl, @ProductName, @CupType, @SugarLevel, @IceLevel, @Quantity, @Price, @TotalPrice, @OrderStatus)";
            
            MySqlParameter[] parameters = {
                new MySqlParameter("@ProductId", order.ProductId),
                new MySqlParameter("@ProductImageUrl", order.ProductImageUrl),
                new MySqlParameter("@ProductName", order.ProductName),
                new MySqlParameter("@CupType", order.CupType),
                new MySqlParameter("@SugarLevel", order.SugarLevel),
                new MySqlParameter("@IceLevel", order.IceLevel),
                new MySqlParameter("@Quantity", order.Quantity),
                new MySqlParameter("@Price", order.Price),
                new MySqlParameter("@TotalPrice", order.TotalPrice),
                new MySqlParameter("@OrderStatus", order.OrderStatus)
            };
            
            return Insert(sql, parameters);
        }

        //获取所有订单（管理员）
        public List<OrderInfo> GetAllOrders()
        {
            string sql = "SELECT OrderId, ProductId, ProductImageUrl, ProductName, CupType, SugarLevel, IceLevel, " +
                         "Quantity, Price, TotalPrice, OrderTime, OrderStatus FROM OrderInfo ORDER BY OrderTime DESC";
            
            return SelectList(sql);
        }

        //根据订单状态获取订单
        public List<OrderInfo> GetOrdersByStatus(int status)
        {
            string sql = "SELECT OrderId, ProductId, ProductImageUrl, ProductName, CupType, SugarLevel, IceLevel, " +
                         "Quantity, Price, TotalPrice, OrderTime, OrderStatus FROM OrderInfo " +
                         "WHERE OrderStatus = @OrderStatus ORDER BY OrderTime DESC";
            
            MySqlParameter[] parameters = {
                new MySqlParameter("@OrderStatus", status)
            };
            
            return SelectList(sql, parameters);
        }

        //更新订单
        public int UpdateOrderStatus(int orderId, int status)
        {
            string sql = "UPDATE OrderInfo SET OrderStatus = @OrderStatus WHERE OrderId = @OrderId";
            
            MySqlParameter[] parameters = {
                new MySqlParameter("@OrderId", orderId),
                new MySqlParameter("@OrderStatus", status)
            };
            
            return Update(sql, parameters);
        }

        //删除订单
        public int DeleteOrder(int orderId)
        {
            string sql = "DELETE FROM OrderInfo WHERE OrderId = @OrderId";
            
            MySqlParameter[] parameters = {
                new MySqlParameter("@OrderId", orderId)
            };
            
            return Delete(sql, parameters);
        }

        //获取订单详情
        public OrderInfo GetOrderById(int orderId)
        {
            string sql = "SELECT OrderId, ProductId, ProductImageUrl, ProductName, CupType, SugarLevel, IceLevel, " +
                         "Quantity, Price, TotalPrice, OrderTime, OrderStatus FROM OrderInfo WHERE OrderId = @OrderId";
            
            MySqlParameter[] parameters = {
                new MySqlParameter("@OrderId", orderId)
            };
            
            return SelectSingle(sql, parameters);
        }
    }
}
